package com.ruoyi.project.remake.controller.api;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.AuthUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.remake.domain.TCaseMain;
import com.ruoyi.project.remake.domain.TRemake;
import com.ruoyi.project.remake.domain.TRemakeCaseDetails;
import com.ruoyi.project.remake.domain.constant.InitConstant;
import com.ruoyi.project.remake.domain.enmu.RemakeStatusState;
import com.ruoyi.project.remake.domain.query.CaseMainFrontQuery;
import com.ruoyi.project.remake.domain.vo.CaseMainVo;
import com.ruoyi.project.remake.service.ITCaseMainService;
import com.ruoyi.project.remake.service.ITRemakeCaseDetailsService;
import com.ruoyi.project.remake.service.ITRemakeService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysConfigService;
import com.ruoyi.project.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 质检Controller
 *
 * @author 孙山伟
 * @date 2020-12-15
 */
@Api("高拍模块接口文档")
@RestController
@RequestMapping("/api/remaked/quality")
public class QualityController extends BaseController {
    @Autowired
    private ITCaseMainService tCaseMainService;
    @Autowired
    private ITRemakeService tRemakeService;
    @Autowired
    private ITRemakeCaseDetailsService tRemakeCaseDetailsService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 待审核质检待列表
     * @param caseMainFrontQuery
     * @return
     */
    @ApiOperation("质检列表")
    @GetMapping("/list")
    public AjaxResult list(CaseMainFrontQuery caseMainFrontQuery)
    {
        TCaseMain tCaseMain=new TCaseMain();
        BeanUtil.copyProperties(caseMainFrontQuery,tCaseMain);
        tCaseMain.setStatus(RemakeStatusState.WAIT_AUDIT.getValue().toString());
        PageHelper.startPage(0, 100, null);
        List<TCaseMain> list = tCaseMainService.selectTCaseMainFrontAllList(tCaseMain);
        return AjaxResult.success(list);
    }

    /**
     * 翻拍人员列表
     * @return 翻拍人员列表
     */
    @ApiOperation("翻拍人员列表")
    @GetMapping("/remakelist")
    public AjaxResult remakelist()
    {
        List<TRemake> list = tRemakeService.selectTRemakeList(new TRemake());
        return AjaxResult.success(list);
    }

    /**
     *  质检程序病例基本信息
     * @param id
     * @return
     */
    @ApiOperation("质检程序病例基本信息")
    @GetMapping("/remakeCaseDetails/{id}")
    public AjaxResult remakeCaseDetails(@PathVariable Long id,String userName)
    {
        TCaseMain tCaseMain=new TCaseMain();
        tCaseMain.setQualityName(userName);
        tCaseMain.setId(id);
        tCaseMainService.updateTCaseMain(tCaseMain);
        return AjaxResult.success(tCaseMainService.selectTCaseMainById(id));
    }

    /**
     *驳回
     * @param id
     * @return
     */
    @ApiOperation("驳回翻拍图片")
    @GetMapping("/reject")
    public AjaxResult reject(Long id,String remark)
    {

        TRemakeCaseDetails tRemakeCaseDetails=tRemakeCaseDetailsService.selectTRemakeCaseDetailsById(id);
        if(!tRemakeCaseDetails.getStatus().equals(RemakeStatusState.WAIT_AUDIT.getValue().toString())){
            return AjaxResult.error("不是待审核状态，不允许提交");
        }
        tRemakeCaseDetails.setStatus(RemakeStatusState.NO_PASS_AUDIT.getValue().toString());
        tRemakeCaseDetails.setRemark(remark);
        tRemakeCaseDetails.setId(id);
        tRemakeCaseDetailsService.updateTRemakeCaseDetails(tRemakeCaseDetails);
        TCaseMain tCaseMain=new TCaseMain();
        tCaseMain.setId(tRemakeCaseDetails.getCaseMainId());
        tCaseMain.setStatus(RemakeStatusState.NO_PASS_AUDIT.getValue().toString());
        tCaseMainService.updateTCaseMain(tCaseMain);
        return AjaxResult.success();
    }

    /**
     *驳回
     * @param id
     * @return
     */
    @ApiOperation("更新图片")
    @GetMapping("/updateImg")
    public AjaxResult updateImg(Long id,String images)
    {

        TRemakeCaseDetails tRemakeCaseDetails=tRemakeCaseDetailsService.selectTRemakeCaseDetailsById(id);
        if(!tRemakeCaseDetails.getStatus().equals(RemakeStatusState.WAIT_AUDIT.getValue().toString())){
            return AjaxResult.error("不是待审核状态，不允许提交");
        }
        TRemakeCaseDetails details=new TRemakeCaseDetails();
        CaseMainVo tCaseMain=tCaseMainService.selectTCaseMainById(tRemakeCaseDetails.getCaseMainId());
        String imagPath=configService.selectConfigByKey(InitConstant.PRO_IMG_KEY) + "/"+ tCaseMain.getCardNumber()+ "/"+images;
        details.setImgPath(imagPath);
        details.setId(id);
        tRemakeCaseDetailsService.updateTRemakeCaseDetails(details);
        return AjaxResult.success();
    }
    /**
     * 通过
     * @param id
     * @return
     */
    @ApiOperation("通过翻拍图片")
    @GetMapping("/pass/{id}")
    public AjaxResult pass(@PathVariable  Long id)
    {

        TRemakeCaseDetails tRemakeCaseDetails=tRemakeCaseDetailsService.selectTRemakeCaseDetailsById(id);
       /* if(tRemakeCaseDetails.getStatus().equals(RemakeStatusState.WAIT_AUDIT.getValue().toString())){
            return AjaxResult.error("不是待审核状态，不允许提交");
        }*/
        tRemakeCaseDetails.setStatus(RemakeStatusState.PASS_AUDIT.getValue().toString());
        tRemakeCaseDetails.setId(id);
        tRemakeCaseDetailsService.updateTRemakeCaseDetails(tRemakeCaseDetails);
        return AjaxResult.success();
    }

    /**
     * 完全通过
     *
     * @param id
     * @return
     */
    @ApiOperation("通过翻拍图片")
    @GetMapping("/completelyPass/{id}/{userId}")
    public AjaxResult completelyPass(@PathVariable Long id,@PathVariable  Long userId) {
        TCaseMain tCaseMain = new TCaseMain();
        tCaseMain.setStatus(RemakeStatusState.PASS_AUDIT.getValue().toString());
        tCaseMain.setId(id);
        TRemakeCaseDetails tRemakeCaseDetails=new TRemakeCaseDetails();
        tRemakeCaseDetails.setCaseMainId(id);
        List<TRemakeCaseDetails> tRemakeCaseDetails1=tRemakeCaseDetailsService.selectTRemakeCaseDetailsList(tRemakeCaseDetails);
        for(TRemakeCaseDetails tRemakeCaseDetails2:tRemakeCaseDetails1){
           /* String status=tRemakeCaseDetails2.getStatus();
           *//* if(!status.equals(RemakeStatusState.PASS_AUDIT.getValue().toString())){
                return AjaxResult.error("存在未审核通过的内容，请核实后在重新提交！");
            }*/
            tRemakeCaseDetails2.setStatus(RemakeStatusState.PASS_AUDIT.getValue().toString());
            tRemakeCaseDetailsService.updateTRemakeCaseDetails(tRemakeCaseDetails2);
        }
        TRemake sysUser=tRemakeService.selectTRemakeById(userId);
        tCaseMain.setRemakeName(null==sysUser?"系统用户":sysUser.getRemakeName());
        tCaseMain.setQualityName(sysUser.getRemakeName());
        tCaseMain.setPagesize(tRemakeCaseDetails1.size());
        tCaseMain.setQualityTime(DateUtils.getNowDate());
        tCaseMainService.updateTCaseMain(tCaseMain);
        return AjaxResult.success();
    }

}
